package com.ChessUtils;


import com.beans.SourceDataSingle;
import com.beans.Message;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 棋子工具类
 *
 * @author acoffee
 * @create 2021-08-28 19:49
 */
public class ChessUtil {

    //写文件的操作
    public static void write(String context, File file) {
        try {
            BufferedWriter br = new BufferedWriter(new FileWriter(file, true));
            br.write(context);
            br.newLine();
            br.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //读文件的操作
    public static List<String> read(File file) {
        //创建储存棋子的集合
        ArrayList<String> strs = new ArrayList();
        //用IO流读取我们保存的棋谱
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String str = null;
            //读取文件
            while ((str = br.readLine()) != null) {

                strs.add(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return strs;
    }

    //默认文件选择路径
    public static FileChooser FileChoose(String titleName) {
        FileChooser fc = new FileChooser();
        fc.setTitle(titleName);
        //设置默认的保存路径
        fc.setInitialDirectory(new File("D:\\棋谱"));

        //设置棋谱文件的后缀名
        fc.getExtensionFilters().add(new FileChooser.ExtensionFilter("棋谱文件的后缀名", "*.csv"));
        return fc;
    }


    //发送消息
    public static void sendMsg(Message msg, Stage stage) {

        //单例
        SourceDataSingle instance = SourceDataSingle.getInstance();

        try {

            //客户端
            Socket socket = new Socket(instance.getTargetIP(), Integer.parseInt(instance.getTargetPort()));

            //输出流
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

            oos.writeObject(msg);

        } catch (Exception e) {
            e.printStackTrace();
            //没有传输成功就提示异常
            ChessUtil.getUpWindows(stage, Alert.AlertType.WARNING, "警告", "网络异常！");
        }

    }

    //弹窗
    public static boolean getUpWindows(Stage stage, Alert.AlertType alertType, String UpWinMessage, String UpWinContext) {
        //弹框
        Alert alert = new Alert(alertType);
        alert.initOwner(stage);
        alert.setTitle("提示");
        alert.setHeaderText(UpWinMessage);
        alert.setContentText(UpWinContext);
        Optional<ButtonType> bt = alert.showAndWait();
        if ((bt.get() == ButtonType.OK)) {
            return true;
        }
        return false;
    }


    //设置按钮对象
    public static Button getButton(String btnName, double btnWidth, double btnHeight, double xPos, double yPos) {

        //创建按钮对象
        Button buttonArg = new Button(btnName);

        //设置按钮的位置
        buttonArg.setPrefSize(btnWidth, btnHeight);

        //定义按钮所在位置
        buttonArg.setLayoutX(xPos);
        buttonArg.setLayoutY(yPos);

        return buttonArg;
    }

    //设置输入框对象
    public static TextField getInputBox(String boxMessage, int xPos, int yPos) {

        TextField inputBox = new TextField();

        //提示信息
        inputBox.setPromptText(boxMessage);
        //输入框位置
        inputBox.setLayoutX(xPos);
        inputBox.setLayoutY(yPos);

        return inputBox;
    }

    //设置标签对象（如输入框标签）
    public static Label getBoxLabel(String labelMessage, double xPos, double yPos) {

        //创建标签及设置标签内容
        Label label = new Label(labelMessage);
        //标签位置
        label.setLayoutX(xPos);
        label.setLayoutY(yPos);

        return label;
    }
}